package com.hkbigdata.sybx.controller;

import com.hkbigdata.sybx.bean.EmergencyContact;
import com.hkbigdata.sybx.mapper.EmergencyContactMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yangjing
 * @creat 2025-06-03-16:49
 * @email 3010335359@qq.com
 */
@RestController
@RequestMapping("/api/emergency")
public class EmergencyContactController {

    @Autowired
    private EmergencyContactMapper contactMapper;

    /**
     * 添加紧急联系人
     */
    @PostMapping("/contacts")
    public Map<String, Object> addContact(@RequestBody EmergencyContact contact) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = contactMapper.insert(contact);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "添加成功");
                result.put("data", contact);
            } else {
                result.put("success", false);
                result.put("message", "添加失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加异常：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 删除紧急联系人
     */
    @DeleteMapping("/delete/{contactId}")
    public Map<String, Object> deleteContact(@PathVariable Long contactId) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = contactMapper.deleteById(contactId);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "删除成功");
            } else {
                result.put("success", false);
                result.put("message", "记录不存在或已删除");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除异常：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据用户ID删除所有紧急联系人
     */
    @DeleteMapping("/users/{userId}/delete")
    public Map<String, Object> deleteContactsByUserId(@PathVariable String userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = contactMapper.deleteByUserId(userId);
            result.put("success", true);
            result.put("message", "删除成功，共删除" + rows + "条记录");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除异常：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 更新紧急联系人信息
     */
    @PutMapping("/update")
    public Map<String, Object> updateContact(@RequestBody EmergencyContact contact) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = contactMapper.update(contact);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "更新成功");
                result.put("data", contact);
            } else {
                result.put("success", false);
                result.put("message", "记录不存在或更新失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新异常：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据ID查询紧急联系人
     */
    @GetMapping("/contacts/{contactId}")
    public Map<String, Object> getContactById(@PathVariable Long contactId) {
        Map<String, Object> result = new HashMap<>();
        try {
            EmergencyContact contact = contactMapper.selectById(contactId);
            if (contact != null) {
                result.put("success", true);
                result.put("message", "查询成功");
                result.put("data", contact);
            } else {
                result.put("success", false);
                result.put("message", "记录不存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询异常：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据用户ID查询所有紧急联系人
     */
    @GetMapping("/users/{userId}/contacts")
    public Map<String, Object> getContactsByUserId(@PathVariable String userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<EmergencyContact> contacts = contactMapper.selectByUserId(userId);
            result.put("success", true);
            result.put("message", "查询成功");
            result.put("data", contacts);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询异常：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 查询所有紧急联系人
     */
    @GetMapping("/all")
    public Map<String, Object> getAllContacts() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<EmergencyContact> contacts = contactMapper.selectAll();
            result.put("success", true);
            result.put("message", "查询成功");
            result.put("data", contacts);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询异常：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
}